package com.blog.controller.admin;

import com.blog.constant.ErrorConstant;
import com.blog.controller.BaseController;
import com.blog.dto.cond.CommentCondition;
import com.blog.model.Comment;
import com.blog.service.comment.CommentService;
import com.blog.utils.ResultVoUtil;
import com.blog.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 评论相关接口
 *
 * @author wmb
 *
 * @since 2021-2-9
 */
@Api("评论相关接口")
@Controller
public class CommentController extends BaseController {

    @Autowired
    @Qualifier("commentService")
    private CommentService commentService;

    @ApiOperation("进入评论列表页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页数", paramType = "query", dataType = "int", defaultValue = "1"),
        @ApiImplicitParam(name = "limit", value = "每页显示条数", paramType = "query", dataType = "int", defaultValue = "10")
    })
    @GetMapping(value = "/admin/comments")
    public String commentIndex(@RequestParam(value = "page", defaultValue = "1", required = false) Integer pageNum,
                               @RequestParam(value = "limit", defaultValue = "15", required = false) Integer pageSize,
                               @ApiIgnore HttpServletRequest request) {
        PageInfo<Comment> commentPageInfo = commentService.getCommentsByCondition(new CommentCondition(), pageNum, pageSize);
        request.setAttribute("comments", commentPageInfo);
        return "admin/comment_list";
    }

    @ApiOperation("删除评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coid", value = "评论数据的主键", paramType = "query", dataType = "int", required = true)
    })
    @PostMapping(value = "/admin/comments/delete")
    @ResponseBody
    public ResultVo deleteComment(@NotNull(message = "评论数据的id不允许为空") @RequestParam("coid") Integer id) {
        commentService.deleteCommentById(id);
        return ResultVoUtil.success();
    }

    @ApiOperation("更改评论状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "coid", value = "评论主键的id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "status", value = "评论状态", paramType = "query", dataType = "String", required = true)
    })
    @PostMapping(value = "/admin/comments/status")
    @ResponseBody
    public ResultVo updateCommentStatus(@NotNull(message = "评论主键的id不允许为空") @RequestParam("coid") Integer id,
                                        @NotBlank(message = "评论状态不允许为空") @RequestParam("status") String status) {
        Comment comment = commentService.getCommentById(id);
        if (comment == null) {
            return ResultVoUtil.error(ErrorConstant.CONTENT_IS_NOT_EXISTS);
        }
        commentService.updateCommentStatus(id, status);
        return ResultVoUtil.success();
    }
}
